<template>
	<view class="jk-page-container bg">
		<jkSearchBar :search-val.sync="searchParams.machineNameOrCode" :searchLeftItemName="searchParams.workshopName"
			:searchRightItemName="searchParams.processName" placeholder="设备编号" @search="onSearchBtn"
			@search-left-item="onClickWorkshop" @search-right-item="onClickProcess"></jkSearchBar>
		<view class="column-list-container">
			<view v-for="(item, index) in tableData" :key="index" class="column-list-item">
				<view class="column-list-item-default flex-between-center">
					<view class="flex-start-center">
						<view class="column-list-item-title">{{ item.machineName || '暂无' }}</view>
						<!--<open-state-bar :status="item.openingState"></open-state-bar>-->
					</view>
					<view class="column-list-item-sub-title">
						<!--<u-button size="mini" type="text" @click="onCancelBtn(item)">取消开台</u-button>-->
						<!--<open-state-bar :status="item.openingState"></open-state-bar>-->
                        <view>{{ `${item.startSpinNumber}~${item.endSpinNumber}` }}</view>
                    </view>
				</view>
				<view class="column-list-item-default column-list-item-sub-title">生产品种：{{ item.productName || '暂无' }}</view>
                <view class="column-list-item-default column-list-item-sub-title">生产批号：{{ item.batchCode || '暂无' }}</view>
                <view class="column-list-item-default column-list-item-sub-title">细纱管色：{{ item.spunYarnColors || '暂无' }}</view>
                <view class="column-list-item-default column-list-item-sub-title">纸管颜色：{{ item.paperTubeList || '暂无' }}</view>
                <view class="column-list-item-default column-list-item-sub-title">通知单号：{{ item.prdNoticeCode || '暂无' }}</view>
                <view class="column-list-item-default column-list-item-sub-title">细纱机台：<text style="white-space: pre-wrap;word-break: break-all;">{{ item.prdNoticeMachine || '暂无' }}</text></view>
				<view style="font-weight: 300;">
					<!--<u-button @click="onToCloseBtn(item)" type="text" size="normal">了机</u-button>-->
				</view>
			</view>
			<jk-no-more :pageNo="searchParams.pageNo" :show="isNoMore" />
		</view>
	</view>
</template>

<script>
	import {
		productionPrdNoticeMachineClearOpeningOpening,
		spinProcessPage,
		systemAuthGetPermissionInfo,
	} from "../../../../api";
	import {
		isLastPage,
		pageConfig,
		showLoading
	} from "../../../../utils/jk-utils";
	import loadingMore from '../../../../mixins/loading-more';
	import jkNoMore from "../../../../jk-components/jk-no-more";
	import jkSearchBar from "../../../../jk-components/jk-search-bar";
	import openStateBar from "../../openMachine/open-state-bar";
    import {productionPrdNoticeLtMachineChangeGetMachineList} from "../../../../api";
	export default {
		mixins: [loadingMore],
		components: {
			jkNoMore,
			jkSearchBar,
			openStateBar
		},
		data() {
			return {
				apiConfig: {
					list: productionPrdNoticeLtMachineChangeGetMachineList
				},
				searchParams: {
					pageNo: 1,
					pageSize: pageConfig.pageSize,
					totalCount: pageConfig.totalCount,
					machineNameOrCode: '',
					workshopId: null,
					workshopName: '',
					processCode: 'XS',
					processName: '',
					openingState: 3
				},
				tableData: [],
				workshopList: [],
				processList: [{
						code: 'XS',
						name: '细纱'
					},
					{
						code: 'CS',
						name: '粗纱'
					},
					{
						code: 'MB',
						name: '末并'
					},
					{
						code: 'TB',
						name: '头并'
					},
					{
						code: 'SM',
						name: '梳棉'
					},
					{
						code: 'QH',
						name: '清花'
					}
				]
			}
		},
		onLoad() {
		},
		onShow() {
			this.getDependentData();
		},
		methods: {
			onCancelBtn(item) {
				let _this = this
				uni.showModal({
					title: '提示',
					content: "确定'取消开台'吗?",
					success: function(res) {
						if (res.confirm) {
							productionPrdNoticeMachineClearOpeningOpening({}, {
								prdNoticeMachineChangeId: item.id
							}).then(res => {
								if (res.data.status === 200) {
									_this.getListRequest();
								}
							});
						}
					}
				});
			},
			onClickProcess() {
				let _this = this;
				/*uni.showActionSheet({
					itemList: _this.processList.map(x => x.name),
					success(res) {
						_this.$set(_this.searchParams, 'processCode', _this.processList[res.tapIndex].code);
						_this.$set(_this.searchParams, 'processName', _this.processList[res.tapIndex].name);
						_this.getListRequest();
					}
				});*/
			},
			onClearOfSearchIpt() {
				this.$set(this.searchParams, 'machineNameOrCode', '');
				this.onSearchBtn();
			},
			onToCloseBtn({
				id
			}) {
				uni.navigateTo({
					url: `/pages/production/closeMachine/toClose?id=${ id }`
				})
			},
			onSearchBtn(e) {
				this.tableData = [];
				this.$set(this.searchParams, 'machineNameOrCode', e);
				this.getListRequest();
			},
			getProcessListRequest() {
				return spinProcessPage({
					auditState: 1
				}).then(res => {
					if (res.data.status === 200) {
						let item = res.data.res.find(x => x.code.toUpperCase() === 'XS');
						if (item) {
							this.$set(this.searchParams, 'processCode', item.code);
							this.$set(this.searchParams, 'processName', item.name);
						}
						this.processList = res.data.res;
					}
				});
			},
			getWorkshopDataRequest() {
				return systemAuthGetPermissionInfo().then(res => {
					if (res.data.status === 200) {
						if (!this.searchParams.workshopId) {
							this.$set(this.searchParams, 'workshopId', res.data.res.workshop.defaultWorkshopId);
							this.$set(this.searchParams, 'workshopName', res.data.res.workshop.defaultWorkshopName);
						}
						this.workshopList = getApp().globalData.defaultWorkshop.list;
					}
				})
			},
			async getDependentData() {
				showLoading(true);
				this.searchParams.pageNo = 1;
				await this.getWorkshopDataRequest();
				if (this.searchParams.workshopId) await this.getListRequest();
				showLoading(false);
			},
			getListRequest() {
				if (this.searchParams.workshopId) {
					showLoading(true);
					this.searchParams.pageNo = 1;
					let params = JSON.parse(JSON.stringify(this.searchParams));
                    params.machineNameOrCode = params.machineNameOrCode.toUpperCase();
                    return this.apiConfig.list(params).then(res => {
						showLoading(false);
						if (res.data.status === 200) {
                            this.$set(this.searchParams, 'totalCount', res.data.count);
                            this.tableData = [];
                            this.setData(res.data.res || []);
							this.isNoMore = isLastPage(this.searchParams.totalCount, this.searchParams.pageSize, this.searchParams.pageNo);
						}
					})
				}
			},
            setData(arr) {
                if (arr && arr.length) {
                    arr.forEach(x => {
                        if (x.productTaskList) {
                            x.productTaskList.forEach(k => {
                                this.tableData.push({
                                    ...x,
                                    prdNoticeMachine: k.prdNoticeMachineList ? k.prdNoticeMachineList.map(x => x.machineCode).join() : '',
                                    productId: k.productId,
                                    productCode: k.productCode,
                                    productName: k.productName,
                                    batchCode: k.batchCode,
                                    prdNoticeCode: k.prdNoticeCode,
                                    spunYarnColors: k.spunYarnColors ? k.spunYarnColors.map(x => x.name).join() : [],
                                    paperTubeList: k.paperTubeList ? k.paperTubeList.map(x => x.name).join() : []
                                })
                            })
                        }
                    })
                }
            },
            getMore() {
                if (!isLastPage(this.searchParams.totalCount, this.searchParams.pageNo, this.searchParams.pageSize)) {
                    this.searchParams.pageNo++;
                    showLoading(true);
                    let params = JSON.parse(JSON.stringify(this.searchParams));
                    params.machineNameOrCode = params.machineNameOrCode.toUpperCase();
                    return this.apiConfig['list'](params).then(res => {
                        showLoading(false);
                        if (res.data.status === 200) {
                            this.$set(this.searchParams, 'totalCount', res.data.count);
                            this.setData(res.data.res || []);
                        }
                    })
                } else {
                    this.isNoMore = true;
                }
            },
			onClickWorkshop() {
				let _this = this;
				uni.showActionSheet({
					itemList: _this.workshopList.map(x => x.workshopName),
					success(res) {
						_this.$set(_this.searchParams, 'workshopId', _this.workshopList[res.tapIndex].workshopId);
						_this.$set(_this.searchParams, 'workshopName', _this.workshopList[res.tapIndex]
							.workshopName);
						_this.getListRequest();
					}
				});
			}
		},
		onReachBottom() {
			this.getMore();
		}
	}
</script>

<style lang="scss" scoped>
</style>
